﻿/********************************************************************************
    Copyright (C) Binod Nepal, Planet Earth Solutions Pvt. Ltd., Kathmandu.
	Released under the terms of the GNU General Public License, GPL, 
	as published by the Free Software Foundation, either version 3 
	of the License, or (at your option) any later version.
    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  
    See the License here <http://www.gnu.org/licenses/gpl-3.0.html>.
***********************************************************************************/

using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;

namespace MixNP.Web.Classifieds.UserControls
{
    public partial class CompactUserProfileControl : System.Web.UI.UserControl
    {
        public string Token { get; set; }


        protected void Page_Load(object sender, EventArgs e)
        {
            if (!string.IsNullOrWhiteSpace(this.Token))
            {
                if (MixNP.BusinessLayer.Users.IsValidToken(this.Token))
                {
                    string emailAddress = MixNP.BusinessLayer.Users.GetEmailAddressFromSalt(this.Token);
                    //TODO
                    string protectProfilePicture = string.Empty;
                    string sex = string.Empty;
                    string userName = string.Empty;
                    string nickName = string.Empty;
                    string cellNumber = string.Empty;
                    string contactNumber = string.Empty;
                    bool displayCellNumber;
                    bool displayContactNumbers;
                    string addressLine1 = string.Empty;
                    string addressLine2 = string.Empty;
                    string addressLine3 = string.Empty;
                    string city = string.Empty;

                    if (!string.IsNullOrWhiteSpace(emailAddress))
                    {
                        using (DataTable table = MixNP.BusinessLayer.Profiles.GetProfile(emailAddress))
                        {
                            if (table.Rows.Count.Equals(1))
                            {

                                if (Pes.Utility.Conversion.TryCastBoolean(table.Rows[0]["ProtectProfilePicture"]).Equals(true))
                                {
                                    protectProfilePicture = "1";
                                }
                                else
                                {
                                    protectProfilePicture = "0";                                
                                }

                                sex = Pes.Utility.Conversion.TryCastString(table.Rows[0]["Sex"]);
                                userName = Pes.Utility.Conversion.TryCastString(table.Rows[0]["Name"]);
                                nickName = Pes.Utility.Conversion.TryCastString(table.Rows[0]["NickName"]);
                                cellNumber = Pes.Utility.Conversion.TryCastString(table.Rows[0]["CellNumber"]);

                                displayCellNumber = Pes.Utility.Conversion.TryCastBoolean(table.Rows[0]["DisplayCellNumber"]);

                                contactNumber = Pes.Utility.Conversion.TryCastString(table.Rows[0]["ContactNumbers"]);

                                displayContactNumbers = Pes.Utility.Conversion.TryCastBoolean(table.Rows[0]["DisplayContactNumbers"]);

                                addressLine1 = Pes.Utility.Conversion.TryCastString(table.Rows[0]["AddressLine1"]);
                                addressLine2 = Pes.Utility.Conversion.TryCastString(table.Rows[0]["AddressLine2"]);
                                addressLine3 = Pes.Utility.Conversion.TryCastString(table.Rows[0]["AddressLine3"]);
                                city = Pes.Utility.Conversion.TryCastString(table.Rows[0]["City"]);


                                NameLiteral.Text = nickName;
                                CellNumberLiteral.Text = "Hidden";
                                ContactNumberLiteral.Text = "Hidden";


                                if (!userName.Equals(nickName))
                                {
                                    NameLiteral.Text = nickName + " (nickname)";
                                }

                                if (displayCellNumber)
                                {
                                    CellNumberLiteral.Text = cellNumber;
                                }

                                if (displayContactNumbers)
                                {
                                    ContactNumberLiteral.Text = contactNumber;
                                }

                                GenderLiteral.Text = sex;

                                AddressLine1Literal.Text = addressLine1;
                                AddressLine2Literal.Text = addressLine2;
                                AddressLine3Literal.Text = addressLine3;
                                CityLiteral.Text = city;


                                string filePath = ResolveUrl("~/Handlers/GetProfilePicture.ashx?Path=" + this.Token + "&RequestId=" + Guid.NewGuid().ToString());


                                StringBuilder html = new StringBuilder();
                                html.Append("<ul class=\"bpad8 thumb\">");
                                html.Append("<li>");
                                html.Append("<a href='" + filePath + "'>");
                                html.Append("<img src='" + filePath + "&H=80&W=80' alt='" + nickName + "' />");
                                html.Append("</a>");
                                html.Append("</li>");
                                html.Append("</ul>");

                                ProfileImageLiteral.Text = html.ToString();

                                return;
                            }
                        }
                    }
                }
            }
            userProfileTable.Visible = false;
            return;

        }
    }
}